//
//  TimeLabel.m
//  K8彩票
//
//  Created by zhoubo on 2017/6/8.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "TimeLabel.h"
#import "Timer.h"

@interface TimeLabel ()

@property (nonatomic,assign) NSInteger  totalTime;
@property (nonatomic,assign) NSInteger  currentTime;

@end



@implementation TimeLabel


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
      }
    return self;
}

-(void)awakeFromNib
{

    [super awakeFromNib];
//    NSTimer* timer = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(dosomething) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    

    Timer* timer = [Timer sharedInstanceTarget:self andSelector:@selector(dosomething)];

    

}

-(void)setText:(NSString *)text
{
    [super setText:text];
    
    
    if(self.totalTime == 0)
    {
        // 这里能获取到显示的时间文字信息
        NSInteger minute = [text substringToIndex:2].integerValue;
        NSInteger second = [text substringFromIndex:3].integerValue;
        self.totalTime = minute*60 + second;
        if (self.currentTime == 0) {
            self.currentTime = self.totalTime;
        }

    }
    else
    {
        return;
    }


    
}

-(void)dosomething
{
    


    self.currentTime -= 1;
    if (self.currentTime <= 0) {
        self.currentTime = self.totalTime;
    }
    
    self.text = [NSString stringWithFormat:@"%02ld:%02ld",self.currentTime/60,self.currentTime%60];
    
}


@end
